<template>
    <transition name="fade">
        <figure ref="oPreview" v-show="show" @click="currentChange">
            <img ref="oImg" :src="<string>srcs[current]" :alt="<string>alts[current]" :style="{ transform: 'translate(-50%, -50%) scale(' + scale + ')' }" />
            <slot name="page">
                <p class="page">{{ current + 1 }} / {{ srcs.length }}</p>
            </slot>
            <slot name="close">
                <i class="close" @click="emits('update:show', false), emits('close', current)">*</i>
            </slot>
            <slot></slot>
        </figure>
    </transition>
</template>

<script lang="ts" setup>
import { ref, onMounted } from "vue";

const props = defineProps({
    show: { type: Boolean, default: false },
    current: { type: Number, default: 0 },
    srcs: { type: Array, default: [] },
    alts: { type: Array, default: [] },
});
const emits = defineEmits(["update:show", "update:current", "change", "close", "prevFail", "nextFail"]);
const oPreview = ref<HTMLElement | null>(null);
const oImg = ref<HTMLElement | null>(null);
let scale = ref(1); // 缩放值
let offsetWidth = 0; // 预览组件的宽度

onMounted(() => {
    if (oPreview.value) {
        oPreview.value.onwheel = (e: WheelEvent) => {
            if (oImg.value) {
                if (e.deltaY > 0) {
                    // 向下滚动
                    scale.value > 0.5 && (scale.value -= 0.2);
                } else {
                    // 向上滚动
                    scale.value < 5 && (scale.value += 0.2);
                }
            }
        };
    }
});

// 改变页码
const currentChange = (e: MouseEvent) => {
    offsetWidth = oPreview.value?.offsetWidth || 0;
    if (e.offsetX < offsetWidth / 2) {
        // 上一页
        if (props.current > 0) {
            scale.value = 1;
            emits("update:current", props.current - 1);
            emits("change", props.current); // 翻页
        } else {
            emits("prevFail"); // 上翻失败
        }
    } else {
        // 下一页
        if (props.current + 1 < props.srcs.length) {
            scale.value = 1;
            emits("update:current", props.current + 1);
            emits("change", props.current); // 翻页
        } else {
            emits("nextFail"); // 下翻失败
        }
    }
};
</script>

<style lang="scss" scoped>
.fade-enter-from {
    opacity: 0;
}
.fade-enter-active {
    transition: all 0.2s;
}
.fade-leave-to {
    opacity: 0;
}
.fade-leave-active {
    position: absolute;
    transition: all 0.2s;
}
figure {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    backdrop-filter: blur(20px) brightness(100%);
    background-color: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: auto;
    cursor: pointer;
    > img {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        transition: all 0.2s;
    }
    .page {
        position: fixed;
        bottom: 50px;
        left: 50%;
        transform: translateX(-50%);
        color: #fff;
        font-size: 20px;
        font-weight: bold;
    }
    .close {
        position: fixed;
        top: 30px;
        right: 30px;
        color: #fff;
        font-size: 40px;
        font-weight: bold;
    }
}
</style>
